/**
 * Created With IntelliJ IDEA
 * Description:leetcode：572. 另一棵树的子树
 * <a href="https://leetcode.cn/problems/subtree-of-another-tree/">...</a>
 * User: DELL
 * Data: 2023-01-14
 * Time: 11:09
 */

class TreeNode {
     int val;
     TreeNode left;
     TreeNode right;
     TreeNode(int val) {
         this.val = val;
     }
 }
//root 树上的节点数量范围是 [1, 2000]
//subRoot 树上的节点数量范围是 [1, 1000]
public class Solution {
    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        if (root == null) {
            return false;
        }
        return isSameTree(root,subRoot)
                || isSubtree(root.left,subRoot)
                || isSubtree(root.right,subRoot);
    }
    private boolean isSameTree(TreeNode t1,TreeNode t2) {
        if (t1 == null && t2 == null) {
            return true;
        }
        if (t1 == null || t2 == null) {
            return false;
        }
        return t1.val == t2.val
                && isSameTree(t1.left,t2.left)
                && isSameTree(t1.right,t2.right);
    }
}